#!/usr/bin/env python
# encoding: utf-8
"""
@author: youfeng
@email: youfeng243@163.com
@license: Apache Licence 
@file: court_searchlist_zhixing_worker.py
@time: 2016/12/7 16:00
"""
import random
import re
import time

from base.court_base_worker import CourtBaseWorker


class CourtSearchListZhiXingWorker(CourtBaseWorker):
    def __init__(self, **kwargs):
        CourtBaseWorker.__init__(self, **kwargs)
        # 抓取url
        self.source_url = 'http://{host}/search/newsearch'.format(host=self.host)
        self.captcha_url = 'http://{host}/search/security/jcaptcha.jpg'.format(host=self.host)
        self.detail_url = 'http://{host}/search/newdetail'.format(host=self.host)

        # 正则初始化
        pattern = ur"^[\u4e00-\u9fa5\w\(\（\)\）\·\.\,\，\、\。]+$"
        self.reg = re.compile(pattern, re.I)

    def get_captcha_url(self, session):
        return self.captcha_url + '?{rand}'.format(rand=random.randint(0, 99))

    # 搜索得到列表信息
    def get_page_html(self, session, keyword, page=1):

        # 暴力破解验证码
        for _ in xrange(10):
            try:
                url = self.source_url
                captcha_no, result_file = self.get_captcha(session, cap_type=3, proxy_type=2)
                if captcha_no is None:
                    time.sleep(1)
                    self.log.error('验证码服务出现异常...')
                    session.proxies = self.get_proxy(proxy_type=2)
                    continue

                data = {'searchCourtName': u'全国法院（包含地方各级法院）',
                        'selectCourtId': 1,
                        'selectCourtArrange': 1,
                        'pname': keyword,
                        'cardNum': '',
                        }
                if page == 1:
                    data['j_captcha'] = captcha_no
                if page > 1:
                    data['currentPage'] = page
                    url = self.source_url + '?j_captcha={cap}'.format(cap=captcha_no)

                r = self.task_request(session.post, url=url, proxy_type=2, data=data)
                if r is None:
                    continue
                if r.text.find('验证码错误') != -1:
                    self.log.warn('当前页面 page = {page} cap = {cap} keyword = {key}'.format(
                        key=keyword, page=page, cap=captcha_no))
                    continue

                return r.text
            except Exception as e:
                self.log.exception(e)

        self.log.error('获取种子页面失败了 验证码失败了..keyword = {keyword} page = {page}'.format(
            keyword=keyword, page=page))
        return None

    def home_page(self, session):
        url = 'http://{host}/search/'.format(host=self.host)
        r = self.task_request(session.get, url, proxy_type=2)
        if r is None:
            self.log.error('首页访问失败...')

    def query_task(self, item):

        if not isinstance(item, dict):
            self.log.info('参数错误: item = {item}'.format(item=item))
            return self.FAIL

        # 判断公司信息字段有没有问题
        company_name = item.get('company_name', '')
        if company_name == '':
            self.log.info('参数错误: 没有company_name字段..item = {item}'.format(item=item))
            return self.FAIL

        # 判断是否需要进行抓取
        if not self.check_crawl_flag(item, self.crawl_flag):
            return self.SUCCESS

        if len(company_name) < 2:
            self.log.warn('公司名称字符太少. company_name = {name}'.format(name=company_name))
            self.set_crawl_flag(item, self.crawl_flag, self.CRAWL_SHORT_NAME)
            return self.FAIL

        # 判断是否符合规格
        if self.reg.match(company_name) is None:
            try:
                self.log.warn('不符合规格的关键字...')
                self.set_crawl_flag(item, self.crawl_flag, self.CRAWL_INVALID_NAME)
            except Exception as e:
                self.log.exception(e)

            return self.FAIL

        self.log.info('开始抓取任务...company = {company}'.format(company=company_name))
        status = self.query_company(company_name)
        self.set_crawl_flag(item, self.crawl_flag, status)
        self.log.info('完成抓取任务...company = {company} status = {status}'.format(company=company_name, status=status))
        if status == self.CRAWL_FINISH:
            return self.SUCCESS
        return self.FAIL
